package com.achievo.know.common;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.ByteBuffer;
import java.nio.channels.Channels;
import java.nio.channels.ReadableByteChannel;

public class Converter {
	
	public static int convertPDF2SWF(final String sourcePath,final  String destPath,final  String fileName) throws IOException {  
        
        //BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(pro.getInputStream())); 
       // String line=null;
        //while ((line=bufferedReader.readLine() )!= null) System.out.println(line);;   
        Thread th = new Thread() {
            public void run() {
            	try {  
            	//目标路径不存在则建立目标路径  
                File dest = new File(destPath);  
                if (!dest.exists()) dest.mkdirs();  
                  
                //源文件不存在则返回  
                File source = new File(sourcePath);  
                if (!source.exists()) return;  
                  
                //调用pdf2swf命令进行转换  
                String command = "D:\\SWFTools\\pdf2swf.exe -T 9 -f -S -p 1-100 " + sourcePath+" -o " + destPath +fileName +" -s languagedir=\"F:\\xpdf\\xpdf-chinese-simplified\" ";  
                System.out.println(command);
                Process pro = Runtime.getRuntime().exec(command);  
                final InputStream ins = pro.getInputStream();
                final ByteBuffer byteBuffer = ByteBuffer.allocate(1024);  
                ReadableByteChannel rbcObj = Channels.newChannel(ins);
                try {
                    while (rbcObj.read(byteBuffer) != -1) {
                        byteBuffer.flip();
                        byteBuffer.clear();
                    }
                } catch (IOException e) {
                	e.printStackTrace();
                }
                
                    pro.waitFor();  
                } catch (Exception e) {  
                    e.printStackTrace();  
                }  
                  
                return;  
            }
        };
        th.setDaemon(true);
        th.start();  
       
        return 1;  
    }  

	/**
	 * @param args
	 * @throws IOException 
	 */
	public static void main(String[] args) throws IOException {
		String sourcePath = "D:\\swftest\\3547817710aa44a8a7667029812bb840.pdf";  
        String destPath = "D:\\swftest\\test\\";  
        String fileName = "%.swf";  
        Converter.convertPDF2SWF(sourcePath, destPath, fileName);

	}

	public static void main1(String[] args) throws Exception {
		String command = "D:\\swftest\\run.bat";  
		Process pro = Runtime.getRuntime().exec(command);  
		/*BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(pro.getInputStream())); 
	    String line=null;
	    while ((line=bufferedReader.readLine() )!= null) System.out.println(line);;   
		pro.waitFor();*/
	}
}
